<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="custom-image" content="https://totemlife.cn/apps/public/solution-cover.jpg">
        <title>营养微生态医学智库</title>
        <script src="/apps/js/baidu-tongji-mtx.js"></script>
        <link rel="stylesheet" href="/apps/css/bootstrap.min.css">
        <link href="/apps/css/fontawesome-free-6.4.0-web/css/all.css" rel="stylesheet">
        <style>
        :root {
            --primary-color: #88B04B;
            --secondary-color: #0F4C81;
            --background-color: #F5FAFF;
            --warning-color: #FF6B6B;
            --primary-color-rgb: 136, 176, 75;
        }
        body {
            font-family: "PingFang SC", "Source Han Sans CN", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: var(--background-color);
            transition: background-image 0.5s ease;
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
        }

        /* 目录导航区域 */
        .nav-container {
            background: rgba(255, 255, 255, 0.95);
            padding: 1rem 0;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .primary-nav {
            position: relative;
            overflow: hidden;
            margin-bottom: 1rem;
        }

        .primary-nav::after {
            content: '';
            position: absolute;
            right: 0;
            top: 0;
            bottom: 0;
            width: 40px;
            background: linear-gradient(to right, rgba(255,255,255,0), rgba(255,255,255,0.95));
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .primary-nav.has-more::after {
            opacity: 1;
        }

        .primary-nav-items {
            display: flex;
            overflow-x: auto;
            scrollbar-width: none;
            -ms-overflow-style: none;
            scroll-behavior: smooth;
            padding: 0.25rem;
        }

        .primary-nav-items::-webkit-scrollbar {
            display: none;
        }

        .primary-nav-item {
            flex: 0 0 auto;
            width: 64px;
            text-align: center;
            padding: 0.5rem 0.25rem;
            margin: 0 0.25rem;
            cursor: pointer;
            transition: all 0.3s ease;
            border-radius: 8px;
        }

        .primary-nav-item.active {
            background-color: rgba(136, 176, 75, 0.1);
        }

        .primary-nav-item img {
            width: 32px;
            height: 32px;
            margin-bottom: 0.25rem;
        }

        .primary-nav-item span {
            display: block;
            font-size: 12px;
            line-height: 1.2;
            color: #333;
            white-space: normal;
            word-break: break-all;
        }

        /* 二级导航区域 */
        .secondary-nav {
            background: rgba(255, 255, 255, 0.95);
            padding: 0.5rem 0;
            border-bottom: 1px solid rgba(15, 76, 129, 0.1);
            position: relative;
        }

        .secondary-nav::after {
            content: '';
            position: absolute;
            right: 0;
            top: 0;
            bottom: 0;
            width: 40px;
            background: linear-gradient(to right, rgba(255,255,255,0), rgba(255,255,255,0.95));
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .secondary-nav.has-more::after {
            opacity: 1;
        }

        .secondary-nav .nav-tabs {
            border: none;
            flex-wrap: nowrap;
            overflow-x: auto;
            scrollbar-width: none;
            -ms-overflow-style: none;
            padding-right: 1rem;
        }

        .secondary-nav .nav-tabs::-webkit-scrollbar {
            display: none;
        }

        .secondary-nav .nav-link {
            border: none;
            padding: 0.5rem 1rem;
            margin: 0 0.25rem;
            border-radius: 20px;
            font-size: 12px;
            white-space: nowrap;
            color: var(--secondary-color);
        }

        .secondary-nav .nav-link:hover,
        .secondary-nav .nav-link.active {
            background-color: var(--primary-color);
            color: white;
        }

        /* 内容区域 */
        .content-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            margin-top: 1rem;
            padding: 1rem;
        }

        /* 卡片区域 */
        .section-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1rem;
            padding: 0.5rem;
            border-bottom: 1px solid rgba(0,0,0,0.1);
        }

        .section-title {
            font-size: 0.9rem;
            color: var(--primary-color);
            margin: 0;
        }

        .section-count {
            font-size: 0.875rem;
            color: var(--primary-color);
            margin-left: 0.5rem;
            background-color: rgba(136, 176, 75, 0.1);
            padding: 0.25rem 0.5rem;
            border-radius: 12px;
            display: inline-block;
        }

        .section-toggle {
            cursor: pointer;
            color: var(--primary-color);
        }

        /* 知识卡片 */
        .knowledge-card {
            background: white;
            border-radius: 12px;
            padding: 1rem;
            margin-bottom: 1rem;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            display: flex;
            gap: 1rem;
        }

        .knowledge-card img {
            width: 96px;
            height: 80px;
            object-fit: cover;
            border-radius: 8px;
        }

        .knowledge-content {
            flex: 1;
        }

        .knowledge-title {
            font-size: 1rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
        }

        .knowledge-summary {
            font-size: 0.875rem;
            color: #666;
            margin-bottom: 0.5rem;
        }

        /* 产品卡片 */
        .product-card {
            background: white;
            border-radius: 12px;
            padding: 1rem;
            margin-bottom: 1rem;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            display: flex;
            flex-direction: column;
            cursor: pointer;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .product-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        }

        .product-title {
            font-size: 0.9rem;
            font-weight: 600;
            margin: 0 0 0.5rem 0;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .product-title-text {
            flex: 1;
            margin-right: 0.5rem;
        }

        .product-body {
            position: relative;
            padding-left: 0;
            overflow: hidden;
            margin-bottom: 0.5rem;
        }

        .product-body::after {
            content: '';
            display: table;
            clear: both;
        }

        .product-image-container {
            width: 96px;
            height: 96px;
            float: left;
            margin-right: 1rem;
            margin-bottom: 0.5rem;
            position: relative;
            border-radius: 8px;
            overflow: hidden;
        }

        .product-card img {
            width: 96px;
            height: 96px;
            object-fit: cover;
            display: block;
            margin: 0;
        }

        .product-name {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            font-size: 0.75rem;
            font-weight: 600;
            text-align: center;
            padding: 0.25rem 0;
            background-color: rgba(var(--primary-color-rgb), 0.9);
            color: white;
        }

        .product-category {
            position: absolute;
            top: 0.5rem;
            right: 0.5rem;
            background-color: rgba(var(--primary-color-rgb), 0.825);
            color: white;
            padding: 0.25rem 0.5rem;
            border-radius: 12px;
            font-size: 0.7rem;
            font-weight: 500;
            z-index: 1;
            opacity: 1;
            transition: opacity 0.3s ease;
            cursor: pointer;
        }

        .product-category:hover {
            background-color: rgba(var(--primary-color-rgb), 1);
        }

        .product-spec {
            color: var(--primary-color);
            font-size: 0.85rem;
            display: inline-block;
            margin-bottom: 0.75rem;
        }

        .product-divider {
            border-bottom: 1px solid rgba(0,0,0,0.1);
            margin-bottom: 0.5rem;
        }

        .product-summary {
            font-size: 0.8rem;
            color: #666;
            line-height: 1.5;
            overflow: visible;
            text-align: justify;
        }

        .product-footer {
            display: flex;
            flex-direction: column;
            margin-top: 0.5rem;
            padding-top: 0.5rem;
            border-top: 1px solid rgba(0, 0, 0, 0.1);
        }

        .product-footer-row {
            display: flex;
            justify-content: space-between;
            width: 100%;
        }

        .product-refs {
            width: auto;
            font-size: 0.75rem;
            color: #666;
        }

        .product-refs-toggle {
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 0.25rem;
            white-space: nowrap;
        }

        .product-refs-list {
            display: none;
            margin-top: 0.5rem;
            width: 100%;
            border-top: 1px dashed rgba(0,0,0,0.1);
            padding-top: 0.5rem;
        }

        .product-refs-list.show {
            display: block;
        }

        .product-footer-refs {
            width: 100%;
            margin-top: 0.5rem;
        }

        .product-refs-item {
            margin-bottom: 0.5rem;
            display: flex;
            align-items: flex-start;
        }

        .product-refs-item:last-child {
            margin-bottom: 0;
        }

        .product-refs-item-number {
            flex-shrink: 0;
            width: 18px;
            color: #888;
            font-size: 0.7rem;
            text-align: right;
        }

        .product-refs-item-title {
            flex: 1;
            overflow: hidden;
            line-height: 1.4;
            font-size: 0.8rem;
            padding-left: 4px;
        }

        .product-refs-item a {
            color: var(--primary-color);
            text-decoration: none;
            display: block;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .product-refs-item a:hover {
            text-decoration: underline;
        }

        .product-stats {
            flex: 1;
            display: flex;
            gap: 1.5rem;
            font-size: 0.75rem;
            color: #666;
            justify-content: flex-end;
        }

        .product-stat {
            display: flex;
            align-items: center;
            gap: 0.25rem;
        }

        /* 咨询区域 */
        .consult-section {
            text-align: center;
            padding: 2rem 1rem;
            background: linear-gradient(135deg, var(--primary-color), #6B66FF);
            color: white;
            border-radius: 12px;
            margin-top: 2rem;
        }

        .consult-title {
            font-size: 1.2rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
        }

        .consult-desc {
            font-size: 0.8rem;
            margin-bottom: 1rem;
        }

        /* 悬浮按钮 */
        <!-- 悬浮按钮相关旧代码已移除，改为使用 float-home-button.js 组件 -->

        /* 添加证据等级说明模态框 */
        .evidence-level-badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0 5px;
            font-size: 10px;
            font-weight: 600;
            color: #FFD700;
            background-color: rgba(0, 0, 0, 0.05);
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            white-space: nowrap;
        }

        .evidence-level-badge i {
            margin: 0 1px;
        }

        .evidence-level-badge:hover {
            background-color: rgba(0, 0, 0, 0.1);
            transform: scale(1.05);
        }
        </style>
    </head>
    <body>
        <!-- 目录导航区域 -->
        <div class="nav-container">
            <div class="container">
                <div class="primary-nav">
                    <div class="primary-nav-items" id="primaryNav"></div>
                </div>
                <div class="secondary-nav">
                    <ul class="nav nav-tabs" id="secondaryNav"></ul>
                </div>
            </div>
        </div>

        <!-- 内容区域 -->
        <div class="container my-4">
            <div class="content-container">
                <!-- 医学案例分享区域 -->
                <div class="section-header">
                    <h2 class="section-title">医学案例与临床研究分享 <span class="section-count">(0)</span></h2>
                    <div class="section-toggle" onclick="toggleSection('knowledge')">
                        <i class="fas fa-chevron-down"></i>
                    </div>
                </div>
                <div class="section-content" id="knowledgeSection" style="display: none;">
                    <div class="row" id="knowledgeList"></div>
                </div>

                <!-- 营养方案推荐区域 -->
                <div class="section-header">
                    <h2 class="section-title">营养微生态调理方案推荐 <span class="section-count">(0)</span></h2>
                    <div class="section-toggle" onclick="toggleSection('products')">
                        <i class="fas fa-chevron-up"></i>
                    </div>
                </div>
                <div class="section-content" id="productsSection">
                    <div class="row" id="productList"></div>
                </div>

                <!-- 咨询区域 -->
                <div class="consult-section">
                    <h3 class="consult-title">需要更多专业建议？</h3>
                    <p class="consult-desc">我们的健康管理团队很高兴为您提供服务</p>
                    <a href="javascript:void(0)" id="consultLink" class="btn btn-light" onclick="return false;">
                        立即咨询 <i class="fas fa-arrow-right ms-2"></i>
                    </a>
                </div>
            </div>
        </div>

        <!-- 悬浮按钮 -->
        <!-- 悬浮按钮相关旧代码已移除，改为使用 float-home-button.js 组件 -->

        <!-- 添加证据等级说明模态框 -->
        <div class="modal fade" id="evidenceLevelModal" tabindex="-1" aria-labelledby="evidenceLevelModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="evidenceLevelModalLabel">方案临床证据质量等级说明</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="d-flex align-items-center mb-3">
                            <p class="mb-0 small text-muted">推荐方案以方案组成成分相关引文，所呈现的临床证据等级最高者为准，动物或体外试验归入其它类型证据，临床证据等级是衡量研究质量的指标，分为以下几种：</p>
                        </div>
                        <div class="d-flex align-items-center mb-3">
                            <div>
                                <strong>高质量证据 ⭐⭐⭐⭐</strong>
                                <p class="mb-0 small text-muted">包括随机对照试验（RCT）和Meta分析（多项RCT的系统评价）。这类研究设计严谨，能最大程度减少偏差，是制定诊疗指南的核心依据。</p>
                            </div>
                        </div>
                        <div class="d-flex align-items-center mb-3">
                            <div>
                                <strong>强质量证据 ⭐⭐⭐</strong>
                                <p class="mb-0 small text-muted">包括小样本RCT和前瞻性队列研究。虽然不如高质量证据严格，但仍能提供较可信的结论，适用于初步验证疗效或长期安全性评估。</p>
                            </div>
                        </div>
                        <div class="d-flex align-items-center mb-3">
                            <div>
                                <strong>回顾性证据 ⭐⭐</strong>
                                <p class="mb-0 small text-muted">包括病例对照研究和横断面研究。这类研究基于已有数据，可能受混杂因素影响，通常用于探索病因或风险因素，而非直接证明疗效。</p>
                            </div>
                        </div>
                        <div class="d-flex align-items-center mb-3">
                            <div>
                                <strong>启发性证据 ⭐</strong>
                                <p class="mb-0 small text-muted">包括病例报告和专家共识。虽然不能单独作为决策依据，但可为罕见病、新疗法或临床经验提供重要线索，推动更深入的研究。</p>
                            </div>
                        </div>
                        <div class="d-flex align-items-center mb-3">
                            <div>
                                <strong>其它类型证据 <i class="fas fa-star-half-alt" style="color: #FFD700;"></i></strong>
                                <p class="mb-0 small text-muted">包括动物试验和体外试验，可以作为非常积极的机制机理验证及临床思路启发。</p>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 将所有JS文件移到这里，并添加defer属性 -->
        <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js" defer></script>
        <script src="/apps/js/bootstrap.bundle.min.js" defer></script>
        <script src="/apps/js/wx-client-dom.js" defer></script>
        <script src="/apps/js/float-home-button.js" defer></script>
        <script>
            let currentCategory = null;
            let categories = [];
            let currentSubCategory = null;

            // 显示证据等级说明模态框
            function showEvidenceLevelModal(event) {
                event.stopPropagation(); // 阻止事件冒泡，避免触发卡片的点击事件
                const modal = new bootstrap.Modal(document.getElementById('evidenceLevelModal'));
                modal.show();
            }

            // 加载分类数据
            async function loadCategories() {
                try {
                    const response = await fetch('/apps/data/diseases/categories');
                    const result = await response.json();
                    if (result.code === 0) {
                        categories = result.data;
                        renderPrimaryNav();
                        if (categories.length > 0) {
                            selectCategory(categories[0]);
                        }
                        checkNavScroll();
                    }
                } catch (err) {
                    console.error('加载分类失败:', err);
                }
            }

            // 检查导航是否需要显示更多提示
            function checkNavScroll() {
                const primaryContainer = document.querySelector('.primary-nav-items');
                const primaryNav = document.querySelector('.primary-nav');
                const secondaryContainer = document.querySelector('.secondary-nav .nav-tabs');
                const secondaryNav = document.querySelector('.secondary-nav');

                if (primaryContainer.scrollWidth > primaryContainer.clientWidth) {
                    primaryNav.classList.add('has-more');
                } else {
                    primaryNav.classList.remove('has-more');
                }

                if (secondaryContainer.scrollWidth > secondaryContainer.clientWidth) {
                    secondaryNav.classList.add('has-more');
                } else {
                    secondaryNav.classList.remove('has-more');
                }
            }

            // 渲染一级导航
            function renderPrimaryNav() {
                const nav = document.getElementById('primaryNav');
                nav.innerHTML = categories.map(cat => `
                <div class="primary-nav-item ${currentCategory && currentCategory.id === cat.id ? 'active' : ''}"
                     onclick="selectCategory(${JSON.stringify(cat).replace(/"/g, '&quot;')})">
                         <img src="${cat.icon_url}" alt="${cat.name}">
                         <span>${cat.name}</span>
                </div>
                `).join('');
             }

            // 渲染二级导航
            function renderSecondaryNav() {
                const nav = document.getElementById('secondaryNav');
                if (!currentCategory) return;

                nav.innerHTML = currentCategory.sub_categories.map(subCat => `
                    <li class="nav-item">
                        <a class="nav-link ${currentSubCategory === subCat ? 'active' : ''}"
                           href="javascript:void(0)"
                           onclick="selectSubCategory('${subCat}')">
                            ${subCat}
                        </a>
                    </li>
                `).join('');
            }

            // 选择二级分类
            function selectSubCategory(subCat) {
                currentSubCategory = subCat;
                renderSecondaryNav();
                loadKnowledge();
                loadProducts();
            }

            // 加载知识卡片
            async function loadKnowledge() {
                try {
                    const tags = encodeURIComponent(`${currentSubCategory}`);
                    const response = await fetch(`/apps/data/posts?tags=${tags}`);
                    const result = await response.json();
                    if (result.code === 0) {
                        renderKnowledge(result.data);
                    }
                } catch (err) {
                    console.error('加载知识卡片失败:', err);
                }
            }

            // 渲染知识卡片
            function renderKnowledge(posts) {
                const container = document.getElementById('knowledgeList');
                container.innerHTML = posts.map(post => `
                    <div class="col-12 col-md-6">
                        <div class="knowledge-card">
                            <img src="${post.image_url}" alt="${post.title}">
                            <div class="knowledge-content">
                                <h3 class="knowledge-title">${post.title}</h3>
                                <p class="knowledge-summary">${post.summary}</p>
                                <a href="${post.detail_url}" class="btn btn-outline-primary btn-sm">
                                    查看详情 <i class="fas fa-arrow-right ms-1"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                `).join('');
                updateSectionCounts();
            }

            // 加载产品数据
            async function loadProducts() {
                try {
                    const tags = encodeURIComponent(`${currentSubCategory}`);
                    const response = await fetch(`/apps/data/products?tags=${tags}`);
                    const result = await response.json();
                    if (result.code === 0) {
                        renderProducts(result.data);
                    }
                } catch (err) {
                    console.error('加载产品失败:', err);
                }
            }

            // 渲染产品卡片
            function renderProducts(products) {
                const container = document.getElementById('productList');
                container.innerHTML = products.map((product, productIndex) => {
                    // 处理参考文献
                    const productId = `product-${productIndex}`;
                    let refsHtml = '';
                    if (product.refs && product.refs.length > 0) {
                        refsHtml = `
                            <div class="product-refs">
                                <div class="product-refs-toggle" onclick="toggleRefs(this, event, '${productId}')">
                                    <span>📚 ${product.refs.length}篇引文</span>
                                    <i class="fas fa-chevron-down"></i>
                                </div>
                            </div>
                        `;
                    }

                    // 根据不同证据等级设置星星数量
                    let evidenceBadge = '';
                    if (product.evidence_level) {
                        let starsHtml = '';

                        if (product.evidence_level.includes('强质量') ) {
                            starsHtml = '<i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i>';
                        } else if (product.evidence_level.includes('高质量') ) {
                            starsHtml = '<i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i>';
                        } else if (product.evidence_level.includes('回顾性') ) {
                            starsHtml = '<i class="fas fa-star"></i><i class="fas fa-star"></i>';
                        } else if (product.evidence_level.includes('启发性') ) {
                            starsHtml = '<i class="fas fa-star"></i>';
                        } else {
                            // 默认显示半颗星
                            starsHtml = '<i class="fas fa-star-half-alt"></i>';
                        }

                        evidenceBadge = `
                            <span class="evidence-level-badge" onclick="showEvidenceLevelModal(event)" title="${product.evidence_level}">
                                ${starsHtml}
                            </span>
                            <i class="fas fa-info-circle" style="color:#88b04b;" onclick="showEvidenceLevelModal(event)" title="${product.evidence_level}"></i>
                        `;
                    }

                    return `
                        <div class="col-12 col-md-6">
                            <div class="product-card" id="${productId}">
                                <h3 class="product-title">
                                    <span class="product-title-text">${product.title}</span>
                                    ${evidenceBadge}
                                </h3>
                                <div class="product-spec" onclick="navi2('${product.detail}', '${product.path}')">${product.spec}</div>
                                <div class="product-body" onclick="navi2('${product.detail}', '${product.path}')">
                                    <div class="product-image-container">
                                        <img src="${product.image}" alt="${product.name}">
                                        <div class="product-name">${product.name}</div>
                                    </div>
                                    <div class="product-summary">${product.summary}</div>
                                </div>
                                <div class="product-footer">
                                    <div class="product-footer-row">
                                        ${refsHtml}
                                        <div class="product-stats">
                                            <div class="product-stat">
                                                <i class="fas fa-eye"></i>
                                                <span>${product.view_count}</span>
                                            </div>
                                            <div class="product-stat">
                                                <i class="fas fa-heart"></i>
                                                <span>${product.like_count}</span>
                                            </div>
                                            <div class="product-stat">
                                                <i class="fas fa-bookmark"></i>
                                                <span>${product.bookmark_count}</span>
                                            </div>
                                            <div class="product-stat">
                                                <i class="fas fa-comment"></i>
                                                <span>${product.comment_count}</span>
                                            </div>
                                        </div>
                                    </div>
                                    ${product.refs && product.refs.length > 0 ? `
                                    <div class="product-footer-refs product-refs-list" id="refs-${productId}">
                                        ${product.refs.map((ref, index) => `
                                            <div class="product-refs-item">
                                                <div class="product-refs-item-number">[${index + 1}]</div>
                                                <div class="product-refs-item-title">
                                                    <a href="${ref.detail}">${ref.title}</a>
                                                </div>
                                            </div>
                                        `).join('')}
                                    </div>
                                    ` : ''}
                                </div>
                            </div>
                        </div>
                    `;
                }).join('');
                updateSectionCounts();
            }

            // 选择分类
            function selectCategory(category) {
                currentCategory = category;
                currentSubCategory = category.sub_categories[0];
                renderPrimaryNav();
                renderSecondaryNav();
                loadKnowledge();
                loadProducts();

                // 更新背景图片
                document.body.style.backgroundImage = `url(${category.background})`;

                // 更新主题色
                document.documentElement.style.setProperty('--primary-color', category.theme_color);
                document.documentElement.style.setProperty('--primary-color-rgb', category.theme_color_rgb);
            }

            // 切换区域显示状态
            function toggleSection(sectionId) {
                const section = document.getElementById(`${sectionId}Section`);
                const toggle = section.previousElementSibling.querySelector('.section-toggle i');
                if (section.style.display === 'none') {
                    section.style.display = 'block';
                    toggle.classList.remove('fa-chevron-down');
                    toggle.classList.add('fa-chevron-up');
                } else {
                    section.style.display = 'none';
                    toggle.classList.remove('fa-chevron-up');
                    toggle.classList.add('fa-chevron-down');
                }
            }

            // 返回小程序
            function navi2(url='https://totemlife.cn/apps/go-to-weapp', path='/pages/index/index') {
                let method = 'navigateTo';
                if ( !url && !path) return;
                if ( url && !path ) return location.href=url;
                if ( path == '/pages/index/index' || path == '/pages/user/user' ) method='switchTab';
                if (window?.wx?.miniProgram) {
                    wx.miniProgram.getEnv((res) => {
                        if (res.miniprogram) {
                            if(method=='navigateTo') {
                                wx.miniProgram.navigateTo({ url: path });
                            }else{
                                wx.miniProgram.switchTab({ url: path });
                            }
                        } else {
                            location.href = url;
                        }
                    });
                } else {
                    location.href = url;
                }
            }

            // 更新区域标题计数
            function updateSectionCounts() {
                const knowledgeCount = document.querySelectorAll('#knowledgeList .knowledge-card').length;
                const productsCount = document.querySelectorAll('#productList .product-card').length;

                const knowledgeTitle = document.querySelector('#knowledgeSection').previousElementSibling.querySelector('.section-title');
                const productsTitle = document.querySelector('#productsSection').previousElementSibling.querySelector('.section-title');

                knowledgeTitle.innerHTML = `临床案例分析${knowledgeCount > 0 ? ` <span class="section-count">${knowledgeCount}</span>` : ''}`;
                productsTitle.innerHTML = `营养方案推荐${productsCount > 0 ? ` <span class="section-count">${productsCount}</span>` : ''}`;
            }

            // 根据标签查找默认分类
            function findDefaultCategoryByTags(tags) {
                if (!tags) return null;

                const decodedTags = decodeURIComponent(tags).split(',');

                // 先尝试匹配一级目录
                for (const tag of decodedTags) {
                    const matchedCategory = categories.find(cat => cat.name === tag);
                    if (matchedCategory) {
                        // 在一级目录下查找匹配的二级目录
                        const matchedSubCategory = matchedCategory.sub_categories.find(subCat =>
                            decodedTags.some(t => subCat === t)
                        );
                        return {
                            category: matchedCategory,
                            subCategory: matchedSubCategory || matchedCategory.sub_categories[0]
                        };
                    }
                }

                // 如果没有匹配的一级目录，尝试匹配二级目录
                for (const tag of decodedTags) {
                    for (const category of categories) {
                        const matchedSubCategory = category.sub_categories.find(subCat => subCat === tag);
                        if (matchedSubCategory) {
                            return {
                                category: category,
                                subCategory: matchedSubCategory
                            };
                        }
                    }
                }

                return null;
            }

            // 切换参考文献显示/隐藏
            function toggleRefs(element, event, productId) {
                event.stopPropagation(); // 阻止事件冒泡，避免触发卡片的点击事件

                const productCard = element.closest('.product-card');
                const refsList = document.getElementById(`refs-${productId}`);
                const icon = element.querySelector('i');

                if (refsList.classList.contains('show')) {
                    refsList.classList.remove('show');
                    icon.classList.remove('fa-chevron-up');
                    icon.classList.add('fa-chevron-down');
                } else {
                    refsList.classList.add('show');
                    icon.classList.remove('fa-chevron-down');
                    icon.classList.add('fa-chevron-up');
                }
            }

            // 页面加载完成后初始化
            document.addEventListener('DOMContentLoaded', () => {
                // 获取URL参数中的tags
                const urlParams = new URLSearchParams(window.location.search);
                const tags = urlParams.get('tags');

                loadCategories().then(() => {
                    if (tags) {
                        const defaultCategory = findDefaultCategoryByTags(tags);
                        if (defaultCategory) {
                            selectCategory(defaultCategory.category);
                            selectSubCategory(defaultCategory.subCategory);
                        }
                    }
                });

                // 监听窗口大小变化
                window.addEventListener('resize', checkNavScroll);
            });

            document.addEventListener('DOMContentLoaded', function() {
                if (typeof initFloatButton === 'function') {
                    initFloatButton();
                }

                // 更新咨询链接
                const home = new URLSearchParams(window.location.search).get('home') || 'totemlife';
                if (typeof window.getHomeJumpInfo === 'function') {
                    const target = window.getHomeJumpInfo(home);
                    const consultLink = document.getElementById('consultLink');
                    if (consultLink && target.helpdesk) {
                        consultLink.href = target.helpdesk;
                        consultLink.onclick = null; // 移除阻止默认行为的 onclick
                    }
                }
            });
        </script>
    </body>
</html>
